package com.hbnu.controller;

import com.hbnu.pojo.User;
import org.springframework.beans.propertyeditors.CustomBooleanEditor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@SessionAttributes("user")
public class HelloServlet {
  //这是一个用于数据绑定的注解，在这里用于注册自定义的编辑器，将日期字符串转换为`Date`对象。
    @InitBinder
    public void dataBinder(WebDataBinder binder){
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat,true));
    }
    //@RequestMapping("/hello"): 处理路径为"/hello"的请求，返回"index.jsp"视图。
    @RequestMapping("/hello")
    public String hello(){
        return "index.jsp";
    }
    //@RequestMapping("/param1"): 处理路径为"/param1"的请求，接受两个参数`param1`和`param2`，并将它们打印出来。
    @RequestMapping("/param1")
    @ResponseBody
    public void paraml(String param1,String param2){
        System.out.println(param1);
        System.out.println(param2);

    }
    //@RequestMapping("/param2"): 处理路径为"/param2"的请求，接受一个`User`对象作为参数，并将该对象打印出来。
    @RequestMapping("/param2")
    @ResponseBody
    public void param2(User user){
        System.out.println(user);
    }
    //@RequestMapping("/param3/{username}/{address}"): 处理路径为"/param3/{username}/{address}"的请求，从路径中提取`username`和`address`参数，并将它们打印出来。
    @RequestMapping("/param3/{username}/{address}")
    @ResponseBody
    public void param3(@PathVariable String username,@PathVariable String address){
        System.out.println(username);
        System.out.println(address);
    }
    //@RequestMapping("/param4"): 处理路径为"/param4"的请求，接受一个`param1`参数，并将其打印出来。
    @RequestMapping("/param4")
    @ResponseBody
    public void param4(@RequestParam String param1){

        System.out.println(param1);
    }
    //@RequestMapping("/param5"): 处理路径为"/param5"的请求，接受一个`Model`对象，并将其打印出来。
    @RequestMapping("/param5")
    @ResponseBody
    public void param5(Model model){
        System.out.println(model);
    }
    //@RequestMapping("/param6")`: 处理路径为"/param6"的请求，使用`@ModelAttribute`注解将`username`作为参数传入，并将其打印出来。
    @RequestMapping("/param6")
    @ResponseBody
    public void param6(@ModelAttribute String username){
        System.out.println(username);
    }
    //@GetMapping("/param7")`: 处理GET请求路径为"/param7"的请求，使用`@RequestHeader`注解提取请求头中的"Accept-Encoding"信息，并将其打印出来。
    @GetMapping("/param7")
    public void handle(
            @RequestHeader("Accept-Encoding") String encoding,
            @RequestHeader("keep-Alive") long keepAlive){
        System.out.println(encoding);
        System.out.println(keepAlive);
    }

    @RequestMapping("/redirect")
    public String redirect(Model model){
        model.addAttribute("username","zhansang");
        return "redirect:/redirect.jsp";
    }

    @RequestMapping("/forward")
    public String forward(Model model){
        model.addAttribute("username","zhansang");
        return "forward:/forward.jsp";
    }

    @RequestMapping("demo1")
    @ResponseBody
    public void demo1(Model model){
        User user=new User();
        user.setUsername("huangs");
        user.setAddress("huangshi");
    //@SessionAttribute("user")将user对象提升为session级别对当前contriller有效
        model.addAttribute("user",user);
    }

    @RequestMapping("demo2")
    @ResponseBody
    public void demo2(Model model){
        User user=(User) model.getAttribute("user");
        System.out.println(user);
    }
    @RequestMapping("demo3")
    @ResponseBody
    public void demo3(HttpServletRequest req, HttpServletResponse resq){
        HttpSession session=req.getSession();
        session.setAttribute("key","value");
    }

    @RequestMapping("demo4")
    @ResponseBody
    public void demo4(Date date){
        System.out.println(date);
    }

    @RequestMapping("demo5")
    @ResponseBody
    public String demo5(){
        return "这是一个中文信息";
    }

    @RequestMapping
    @ResponseBody
    public void demo6(){

    }
}
